package com.rul.mars.product.controller;

import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ProductQueryParam;
import com.rul.mars.mbg.model.Product;
import com.rul.mars.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RuL
 */
@Api(tags = "AdminProductController", description = "管理员商品服务接口")
@RestController
@RequestMapping("/admin/product")
public class AdminProductController {

    @Autowired
    private ProductService productService;


    /**
     * 分页查询商品
     *
     * @param queryParam    查询参数
     * @param pageNum  页面号
     * @param pageSize 页面大小
     * @return 商品分页信息
     */
    @ApiOperation("分页查询商品信息")
    @GetMapping("/list")
    public CommonResult<CommonPage<Product>> listProduct(@RequestBody ProductQueryParam queryParam,
                                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        //商品信息
        List<Product> productList = productService.listProduct(queryParam, pageNum, pageSize);
        //转为分页信息
        CommonPage<Product> productPage = CommonPage.restPage(productList);
        return CommonResult.success(productPage);
    }


    /**
     * 商品审核
     *
     * @param id     商品ID
     * @param status 审核状态（0-未通过，1-通过）
     * @return 审核结果
     */
    @ApiOperation("管理员审核审核商品")
    @PostMapping("/verify")
    public CommonResult verifyProduct(@RequestParam("id") Long id,
                                      @RequestParam("status") Byte status) {
        boolean success = productService.verifyProduct(id, status);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 管理员删除商品
     *
     * @param id 商品id
     * @return 删除结果
     */
    @ApiOperation("管理员删除商品")
    @PostMapping("/delete")
    public CommonResult deleteProduct(@RequestParam("id") Long id) {
        boolean success = productService.deleteProduct(id);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
